﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Utils;
using System.Data;
using Utils.DatabaseUtils;
using System.Data.SqlClient;

namespace DCSLibrary
{
    public class UsersUI
    {
        public DataSet GetAll()
        {
            try
            {
                return SqlHelper.ExecuteDataset(Common.Conn, CommandType.StoredProcedure, "Users_getall");
            }
            catch (Exception e)
            {
                throw new Exception(e.ToString());
            }
        }

        public DataSet GetByID(int UserID)
        {
            try
            {
                return SqlHelper.ExecuteDataset(Common.Conn, "Users_get",UserID);
            }
            catch (Exception e)
            {
                throw new Exception(e.ToString());
            }
        }

        public DataSet GetByUserType(byte userType)
        {
            try
            {
                return SqlHelper.ExecuteDataset(Common.Conn, "Users_getbytype", userType);
            }
            catch (Exception e)
            {
                throw new Exception(e.ToString());
            }
        }

        public DataSet GetByEmail(string email)
        {
            try
            {
                return SqlHelper.ExecuteDataset(Common.Conn, "Users_getbyemail", email);
            }
            catch (Exception e)
            {
                throw new Exception(e.ToString());
            }
        }

        public int DeleteByID(int UserID)
        {
            try
            {
                return SqlHelper.ExecuteNonQuery(Common.Conn, "Users_get", UserID);
            }
            catch (Exception e)
            {
                throw new Exception(e.ToString());
            }
        }

        public int Add(String UserName, String Email, String FirstName, String LastName, DateTime Birthday, byte Sex, String Mobile,
            String Address, DateTime RegDate, String Password, bool IsActive, String ActiveCode, DateTime LastLogin,
            String LoginProcess, byte UserType )
        {
            try
            {
                return Convert.ToInt32(SqlHelper.ExecuteScalar(Common.Conn, "Users_add", UserName, Email, FirstName, LastName, Birthday, Sex, Mobile, Address, RegDate, Password, IsActive, ActiveCode,
                    LastLogin,LoginProcess,UserType));
            }
            catch (Exception e)
            {
                throw new Exception(e.ToString());
            }
        }

        public int Update(int UserID, String UserName, String Email, String FirstName, String LastName, DateTime Birthday, byte Sex, String Mobile,
            String Address,String CompanyName, String CompanyAddress, String Fax, String Position, String Job)
        {
            try
            {
                return SqlHelper.ExecuteNonQuery(Common.Conn, "Users_update", UserID, UserName, Email, FirstName, LastName, Birthday, Sex, Mobile, Address,
                 CompanyName, CompanyAddress, Fax, Position, Job);
            }
            catch (Exception e)
            {
                throw new Exception(e.ToString());
            }
        }

        public int Add(String UserName, String Email, String FirstName, String LastName, DateTime Birthday, byte Sex, String Mobile,
            String Address, DateTime RegDate, String Password, bool IsActive, String ActiveCode, DateTime LastLogin,
            String CompanyName, String CompanyAddress, String Fax, String Position, String Job, String LoginProcess, byte UserType)
        {
            try
            {
                return Convert.ToInt32(SqlHelper.ExecuteScalar(Common.Conn, "Users_add", UserName, Email, FirstName, LastName, Birthday, Sex, Mobile, Address, RegDate, Password, IsActive, ActiveCode,
                    LastLogin, CompanyName, CompanyAddress, Fax, Position, Job, LoginProcess, UserType));
            }
            catch (Exception e)
            {
                throw new Exception(e.ToString());
            }
        }

        /// <summary>
        /// Ham thay doi mat khau
        /// </summary>
        /// <param name="UserID"></param>
        /// <param name="OldPassword"></param>
        /// <param name="NewPassword"></param>
        /// <returns>
        /// Tra ve > 0 tuc la thay doi mat khau thanh cong, neu ko thi se la mat khau khong hop le
        /// </returns>
        public int ChangePassword(int UserID, String OldPassword, String NewPassword)
        {
            try
            {
                return Convert.ToInt32(SqlHelper.ExecuteScalar(Common.Conn, "Users_ChangePassword", UserID, OldPassword, NewPassword));
            }
            catch (Exception e)
            {
                throw new Exception(e.ToString());
            }
        }

        public int SetActiveCodeRetakePassword(int UserID, string ActiveCode)
        {
            try
            {
                return Convert.ToInt32(SqlHelper.ExecuteNonQuery(Common.Conn, "Users_PreRetakePassword", UserID, ActiveCode));
            }
            catch (Exception e)
            {
                throw new Exception(e.ToString());
            }
        }

        public DataSet RetakePassword(String Code, String NewPassword)
        {
            try
            {
                return SqlHelper.ExecuteDataset(Common.Conn, "Users_RetakePassword", Code, NewPassword);
            }
            catch (Exception e)
            {
                throw new Exception(e.ToString());
            }
        }

        public int UpdateRole(int userId, int roleId)
        {
            try
            {
                return SqlHelper.ExecuteNonQuery(Common.Conn, "Users_updaterole", userId, roleId);
            }
            catch (Exception e)
            {
                return -1;
            }
        }

        public int UpdateMoney(String userName, decimal money, String type)
        {
            try
            {
                return SqlHelper.ExecuteNonQuery(Common.Conn, "MoneyLog_AddByName", userName, money, type);
            }
            catch (Exception e)
            {
                return -1;
            }
        }

        public int Active(String userName)
        {
            try
            {
                return SqlHelper.ExecuteNonQuery(Common.Conn, "Users_Active", userName);
            }
            catch (Exception e)
            {
                return -1;
            }
        }

        public int SetUserType(string UserName, string Password, int vipDay, byte UserType, bool isCreateNew)
        {
            try
            {
                return Convert.ToInt32(SqlHelper.ExecuteScalar(Common.Conn, "Users_SetUserType", UserName, Password, vipDay, UserType, isCreateNew));
            }
            catch (Exception e)
            {
                throw new Exception(e.ToString());
            }
        }
    }

    
}
